<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <script src="https://cdn.jsdelivr.net/npm/echarts@5.4.0/dist/echarts.min.js"></script>
  <script src="https://cdn.jsdelivr.net/npm/vue@2/dist/vue.js"></script>
  <script src="https://cdn.jsdelivr.net/npm/axios/dist/axios.min.js"></script>
  <title>Document</title>
  <style>
    .red {
      color: red !important;
    }

    .search {
      width: 300px;
      margin: 20px 0;
    }

    .my-form {
      display: flex;
      margin: 20px 0;
    }

    .my-form input {
      flex: 1;
      margin-right: 20px;
    }

    .table> :not(:first-child) {
      border-top: none;
    }

    .contain {
      display: flex;
      padding: 10px;
    }

    .list-box {
      flex: 1;
      padding: 0 30px;
    }

    .list-box a {
      text-decoration: none;
    }

    .echarts-box {
      width: 600px;
      height: 400px;
      padding: 30px;
      margin: 0 auto;
      border: 1px solid #ccc;
    }

    tfoot {
      font-weight: bold;
    }

    @media screen and (max-width: 1000px) {
      .contain {
        flex-wrap: wrap;
      }

      .list-box {
        width: 100%;
      }

      .echarts-box {
        margin-top: 30px;
      }
    }
  </style>
</head>

<body>
  <div id="app">
    <div class="contain">
      <!-- 左侧列表 -->
      <div class="list-box">

        <!-- 添加资产 -->
        <form class="my-form">
          <input type="text" class="form-control" placeholder="消费名称" v-model.trim="newName" />
          <input type="number" class="form-control" placeholder="消费价格" v-model.number="newPrice" />
          <button type="button" class="btn btn-primary" @click="addBill()">添加</button>
        </form>

        <table class="table table-hover">
          <thead>
            <tr>
              <th>编号</th>
              <th>消费名称</th>
              <th>消费价格</th>
              <th>操作</th>
            </tr>
          </thead>
          <tbody>
            <tr v-for="(item, index) in billList" :key="item.id">
              <td>{{index}}</td>
              <td>{{item.name}}</td>
              <td>{{item.price}}</td>
              <td><a href="javascript:;" @click="deleteBill(item.id)">删除</a></td>
            </tr>

          </tbody>
          <tfoot>
            <tr>
              <td colspan="4">消费总计： {{totalPrice}}</td>
            </tr>
          </tfoot>
        </table>
      </div>

      <!-- 右侧图表 -->
      <div class="echarts-box" id="main"></div>
    </div>
  </div>

</body>
<script>

  var app = new Vue({
    el: '#app',
    data: {
      billList: [],
      newName: '',
      newPrice: 0
    },
    methods: {
      async deleteBill (index) {
        // 根据 id 发送删除请求
        const res = await axios.delete(`https://applet-base-api-t.itheima.net/bill/${id}`)
        // 重新渲染
        this.getList()
      }
      ,
      async addBill (index) {
        var res = await axios.post('https://applet-base-api-t.itheima.net/bill', {
          name: this.newName,
          price: this.newPrice,
          creator: 'jack'
        })

        this.newName = ''
        this.newPrice = 0
        await this.getBillList()
      }
      ,
      async getBillList () {
        var res = await axios.get('https://applet-base-api-t.itheima.net/bill', {
          params: {
            creator: 'jack'
          }
        })
        console.log(res.data)
        this.billList = res.data.data
        // 更新图表
        this.myChart.setOption({
          // 数据项
          series: [
            {
              // data: [
              //   { value: 1048, name: '球鞋' },
              //   { value: 735, name: '防晒霜' }
              // ]
              data: this.billList.map(item => ({ value: item.price, name: item.name }))
            }
          ]
        })
      }

    },
    computed: {
      totalPrice () {
        var total = 0
        this.billList.forEach(item => { total += item.price })
        return total.toFixed(2)
      }
    },
    async created () {
      await this.getBillList()
    },
    mounted () {
      // 绘制图表
      this.myChart = echarts.init(document.querySelector('#main'))
      this.myChart.setOption({
        // 大标题
        title: {
          text: '消费账单列表',
          left: 'center'
        },
        // 提示框
        tooltip: {
          trigger: 'item'
        },
        // 图例
        legend: {
          orient: 'vertical',
          left: 'left'
        },
        // 数据项
        series: [
          {
            name: '消费账单',
            type: 'pie',
            radius: '50%', // 半径
            data: [

            ],
            emphasis: {
              itemStyle: {
                shadowBlur: 10,
                shadowOffsetX: 0,
                shadowColor: 'rgba(0, 0, 0, 0.5)'
              }
            }
          }
        ]
      })
    }
  })
</script>



</html>